#include "smallerShare.h"
#include "math.h"
smallerShare::smallerShare() {}

void smallerShare::setStyle(const QString &style) {
  do {
    if (style.isEmpty())
      break;
    QFile qss(style);
    if (false == qss.open(QIODevice::ReadOnly | QIODevice::Text))
      break;
    qApp->setStyleSheet(qss.readAll());
    // qApp->setStyle();
    qss.close();
  } while (0);
}

void smallerShare::writeFile(QString fileName, QString data) {
  if (fileName.isEmpty() == true) {
    return;
  }
  QFile file(fileName);
  if (file.open(QIODevice::Text | QIODevice::Append | QIODevice::WriteOnly) ==
      false) {
    QMessageBox::information(NULL, QString("错误:"),
                             QString("文件打开失败:") + file.errorString());
    return;
  }
  QTextStream output(&file);
  output << data;
  file.flush();
  file.close();
}

QString smallerShare::hex2QString(QByteArray disArray) {
  #if 0
  if (disArray.length() == 0)
    return QString("");

  QString str = QString(disArray.toHex().toUpper());
  QString tmp, disTmp;
  /* 自己加空格 有点麻烦 */
  for (int i = 0; i < str.length(); i += 2) {
    tmp.clear();
    tmp = str.mid(i, 2);
    disTmp += tmp;
    disTmp += " ";
  }
    return disTmp;
  #endif
  if (disArray.length() == 0) return QString("");
  /* 此处直接使用QByteArray方法先插入转为hex码,插入空格 然后转为大写返回 */
  QString str = disArray.toHex(' ').toUpper();
  return str;

}

char smallerShare::ConvertHexChar(char ch) {
  if ((ch >= '0') && (ch <= '9')) {
    return ch - 0x30;
  } else if ((ch >= 'A') && (ch <= 'F')) {
    return ch - 'A' + 10;
  } else if ((ch >= 'a') && (ch <= 'f')) {
    return ch - 'a' + 10;
  } else {
    return (-1);
  }
}

QByteArray smallerShare::QString2Hex(QString str) {
  QByteArray senddata;
  int hexdata, lowhexdata;
  int hexdatalen = 0;
  int len = str.length();

  if (0 != (len % 2)) {
    len += 1;
    str += "0";
  }

  senddata.resize(len / 2);
  char lstr, hstr;
  for (int i = 0; i < len;) {
    hstr = str[i].toLatin1(); //字符型
    if (hstr == ' ') {
      i++;
      continue;
    }
    i++;
    if (i >= len)
      break;
    lstr = str[i].toLatin1();
    hexdata = ConvertHexChar(hstr);
    lowhexdata = ConvertHexChar(lstr);
    if ((hexdata == 16) || (lowhexdata == 16))
      break;
    else
      hexdata = hexdata * 16 + lowhexdata;
    i++;
    senddata[hexdatalen] = (char)hexdata;
    hexdatalen++;
  }
  senddata.resize(hexdatalen);
  return senddata;
}

void smallerShare::addEchanged(uint8_t *data_in, uint8_t *data_out,
                               uint8_t len) {
  int i = 0;
  uint8_t tmp;
  memcpy(data_out, data_in, len);
  if ((len % 2) == 0) {
    for (i = 0; i < ((uint8_t)len / 2); i++) {
      tmp = data_out[i];
      data_out[i] = data_in[len - 1 - i];
      data_out[len - 1 - i] = tmp;
    }
  } else {
    for (i = 0; i < (uint8_t)((len + 1) / 2); i++) {
      if (i == (uint8_t)((len + 1) / 2)) {
        break;
      }
      tmp = data_out[i];
      data_out[i] = data_out[len - 1 - i];
      data_out[len - 1 - i] = tmp;
    }
  }
}

void smallerShare::removeLayout(QWidget *wdialog, QLayout *layout) {
  wdialog->hide();
  QLayoutItem *child;
  if (layout == NULL)
    return;
  while ((child = layout->takeAt(0)) != NULL) {
    if (child->widget()) {
      child->widget()->setParent(NULL);
      delete child->widget(); /* 此处漏了 删除,会造成内存泄露 */
    } else if (child->layout()) {
      removeLayout(NULL, child->layout());
    }
    delete child;
  }
  delete layout;
}

bool copyFileMap(QString m_strInputFile, QString m_strOutputFile) {
  QFile objInputFile(m_strInputFile);
  if (!objInputFile.open(QIODevice::Unbuffered | QIODevice::ReadOnly)) {
    return false;
  }

  QFile objOutputFile(m_strOutputFile);
  if (!objOutputFile.open(QIODevice::Unbuffered | QIODevice::ReadWrite |
                          QIODevice::Truncate)) {
    objInputFile.close();
    return false;
  }

  uchar *pInputData;
  uchar *pOutputData;
  quint64 m_unBlockSize = 10 * 1024 * 1024;
  quint64 m_qint64FileOffset = 0;
  quint64 m_qint64InputFileSize = objInputFile.size();
  objOutputFile.resize(m_qint64InputFileSize);
  int unTmpBlockSize = 0;
  while (m_qint64FileOffset < m_qint64InputFileSize) {
    if (m_qint64InputFileSize - m_qint64FileOffset >= m_unBlockSize) {
      unTmpBlockSize = m_unBlockSize;
    } else {
      unTmpBlockSize = m_qint64InputFileSize - m_qint64FileOffset;
    }

    pInputData = objInputFile.map(m_qint64FileOffset, unTmpBlockSize);
    if (pInputData == NULL) {
      objInputFile.close();
      objOutputFile.close();
      return false;
    }

    pOutputData = objOutputFile.map(m_qint64FileOffset, unTmpBlockSize);
    if (pOutputData == NULL) {
      objInputFile.close();
      objOutputFile.close();
      return false;
    }

    memcpy(pOutputData, pInputData, unTmpBlockSize);
    m_qint64FileOffset += unTmpBlockSize;

    objInputFile.unmap(pInputData);
    objOutputFile.unmap(pOutputData);
  }
  objInputFile.close();
  objOutputFile.close();
  return true;
}

bool readFileByPos(QFile &file, QByteArray &outData, quint64 readSize,
                   quint64 pos) {
  bool bRet = false;
  byte *sPtr = NULL;
  quint64 fileSize;
  do {
    fileSize = file.size();
    /* 文件空 */
    if (fileSize == 0)
      break;
    /* pos超出文件大小 */
    if (fileSize < pos)
      break;
    sPtr = file.map(pos, readSize);
    outData.append((char *)sPtr, readSize);
    bRet = true;
  } while (0);
  return bRet;
}
